import { t } from 'i18next'
import { Mt, MtEntity, revert_mt } from '@/api/manager/mt'
import { useActionButtonsStore } from './blocks/main/actionButtons/store'
import { getCurrentTabType, useParamsStore } from './blocks/main/store'
import { useTableAllStore } from './blocks/main/table/all/store'
import { getItemInTree, getVisibleItemInTree } from './blocks/main/table/all/utils'
import { useTableDcsStore } from './blocks/main/table/dcs/store'
import { useTableInteractiveStore } from './blocks/main/table/interactive/store'
import { useTableOftenStore } from './blocks/main/table/often/store'
import { useTableOrgStore } from './blocks/main/table/organize/store'
import { useMccStore } from './store'
import { Meeting } from '@/api/manager/meeting'
import Alert, { Confirm } from '@/components/Alert'
import { fetchDeleteMt, fetchDeleteOnlineMt } from '@/api/manager/actions'
import { fetchDeleteDcsMts, fetchHangupDcsMts } from '@/api/manager/dcs'
import { useMccMtVideoStore } from './blocks/mccMtVideo/store'
import { useMixStore } from './blocks/mix/store'
import { useVipStore } from './blocks/vip/store'
import { useSmsStore } from './blocks/sms/store'
import { useChasingStore } from './blocks/chasing/store'
import { usePollStore } from './blocks/poll/store'
import { useRollCallStore } from './blocks/roll/store'
//全选
export const actionButtonsCheckAll = () => {
	const screenList = useActionButtonsStore.getState().screenList
	if (getCurrentTabType() === 'all') {
		const tree = useTableAllStore.getState().tree
		getVisibleItemInTree(tree, (item) => {
			const superMtNum = useMccStore.getState().mccInfo.superMtNum
			// const superMtNum = window.ConfigStatic.mccInfo.superMtNum // 超大方
			const flag = screenList.includes('hangUp') || screenList.includes('delete') // 操作类型
			if ((item.mcc.isMt || (!item.mcc.isMt && !item.mcc.status.isOnline)) && item.status.isMatchSearch) {
				if (superMtNum === 1) {
					if (flag) {
						item.status.isChecked = true
					} else {
						if (item.mcc.isMt && item.mcc.status.isInteractive) {
							item.status.isChecked = true
						} else {
							item.status.isChecked = false
						}
					}
				} else {
					item.status.isChecked = true
				}
			}
		})
		useTableAllStore.setState({
			tree: { ...tree },
		})
	} else if (getCurrentTabType() === 'often') {
		const list = useTableOftenStore.getState().list
		for (const item of list) {
			if (item.status.isMatchSearch) {
				item.status.isChecked = true
			}
		}
		useTableOftenStore.setState({
			list: [...list],
		})
	} else if (getCurrentTabType() === 'interactive') {
		const list = useTableInteractiveStore.getState().list
		for (const item of list) {
			if (item.status.isMatchSearch) {
				item.status.isChecked = true
			}
		}
		useTableInteractiveStore.setState({
			list: [...list],
		})
	} else if (getCurrentTabType() === 'dcs') {
		const list = useTableDcsStore.getState().list
		const superMtNum = useMccStore.getState().mccInfo.superMtNum
		// const superMtNum = window.ConfigStatic.mccInfo.superMtNum //超大方
		const flag = screenList.includes('hangUp') || screenList.includes('delete') // 操作类型
		for (const item of list) {
			if (item.status.isMatchSearch) {
				if (superMtNum === 1) {
					if (flag) {
						item.status.isChecked = true
					} else {
						if (item.mcc.status.isInteractive) {
							item.status.isChecked = true
						} else {
							item.status.isChecked = false
						}
					}
				} else {
					item.status.isChecked = true
				}
			}
		}
		useTableDcsStore.setState({
			list: [...list],
		})
	} else if (getCurrentTabType() === 'org') {
		const list = useTableOrgStore.getState().list
		const superMtNum = useMccStore.getState().mccInfo.superMtNum
		// const superMtNum = window.ConfigStatic.mccInfo.superMtNum //超大方
		const flag = screenList.includes('hangUp') || screenList.includes('delete') // 操作类型
		for (const item of list) {
			if (item.status.isMatchSearch && item.status.isMatchOrgTree) {
				if (superMtNum === 1) {
					if (flag) {
						item.status.isChecked = true
					} else {
						if (item.mcc.status.isInteractive) {
							item.status.isChecked = true
						} else {
							item.status.isChecked = false
						}
					}
				} else {
					item.status.isChecked = true
				}
				if (!item.mcc.mtid && item.mcc.confLevel !== 2 /**1:终端离线 2:终端不属于当前会议 */) {
					item.status.isChecked = false /**终端不加入选中列表 */
				}
			}
		}
		useTableOrgStore.setState({
			list: [...list],
		})
	}
	//切换按钮名
	const switchCheckOrUnCheckAll = useActionButtonsStore.getState().switchCheckOrUnCheckAll
	switchCheckOrUnCheckAll('unCheckAll')
}
//取消全选
export const actionButtonsUnCheckAll = () => {
	if (getCurrentTabType() === 'all') {
		const tree = useTableAllStore.getState().tree
		getItemInTree(tree, (item) => {
			if (item.mcc.isMt || (!item.mcc.isMt && !item.mcc.status.isOnline)) {
				item.status.isChecked = false
			}
		})
		useTableAllStore.setState({
			tree: { ...tree },
		})
	} else if (getCurrentTabType() === 'often') {
		const list = useTableOftenStore.getState().list
		for (const item of list) {
			item.status.isChecked = false
		}
		useTableOftenStore.setState({
			list: [...list],
		})
	} else if (getCurrentTabType() === 'interactive') {
		const list = useTableInteractiveStore.getState().list
		for (const item of list) {
			item.status.isChecked = false
		}
		useTableInteractiveStore.setState({
			list: [...list],
		})
	} else if (getCurrentTabType() === 'dcs') {
		const list = useTableDcsStore.getState().list
		for (const item of list) {
			item.status.isChecked = false
		}
		useTableDcsStore.setState({
			list: [...list],
		})
	} else if (getCurrentTabType() === 'org') {
		const list = useTableOrgStore.getState().list
		for (const item of list) {
			item.status.isChecked = false
		}
		useTableOrgStore.setState({
			list: [...list],
		})
	}
	//切换按钮名
	const switchCheckOrUnCheckAll = useActionButtonsStore.getState().switchCheckOrUnCheckAll
	switchCheckOrUnCheckAll('checkAll')
}
export const actionButtonsCancel = () => {
	// actionButtonsUnCheckAll()
	useParamsStore.getState().hideActionButton()
}
//挂断
export const actionButtonsHangupMts = () => {
	let resultMts: Mt[] = []
	let resultMeetings: Meeting[] = []
	let dcsList: Mt[] = []
	if (getCurrentTabType() === 'all') {
		const tree = useTableAllStore.getState().tree
		getItemInTree(tree, (item) => {
			if (item.mcc.isMt) {
				if (item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			} else if (!item.mcc.isMt && !item.mcc.status.isOnline) {
				if (item.status.isChecked) {
					resultMeetings.push(item.mcc)
				}
			}
		})
	} else if (getCurrentTabType() === 'often') {
		const list = useTableOftenStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'interactive') {
		const list = useTableInteractiveStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'dcs') {
		const list = useTableDcsStore.getState().list
		dcsList = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'org') {
		const list = useTableOrgStore.getState().list
		for (const item of list) {
			if (item.status.isChecked) {
				if (item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			}
		}
	}
	if (resultMts.length === 0 && resultMeetings.length === 0 && dcsList.length === 0) {
		Alert(t('请先选择需要挂断的终端'))
		return
	}
	let num = resultMts.length + resultMeetings.length + dcsList.length
	Confirm(t('是否挂断已选择的 {{num}} 个终端？', { num: num }), () => {
		const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
		if (getCurrentTabType() === 'dcs') {
			fetchHangupDcsMts(meetingMoid, dcsList)
		} else {
			fetchDeleteOnlineMt(meetingMoid, [...resultMts, ...resultMeetings])
		}
	})
}
//删除
export const actionButtonsDelete = () => {
	let resultMts: Mt[] = []
	let resultMeetings: Meeting[] = []
	let dcsList: Mt[] = []
	if (getCurrentTabType() === 'all') {
		const tree = useTableAllStore.getState().tree
		getItemInTree(tree, (item) => {
			if (item.mcc.isMt) {
				if (item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			} else if (!item.mcc.isMt && !item.mcc.status.isOnline) {
				if (item.status.isChecked) {
					resultMeetings.push(item.mcc)
				}
			}
		})
	} else if (getCurrentTabType() === 'often') {
		const list = useTableOftenStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'interactive') {
		const list = useTableInteractiveStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'dcs') {
		const list = useTableDcsStore.getState().list
		dcsList = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'org') {
		const list = useTableOrgStore.getState().list
		for (const item of list) {
			if (item.status.isChecked) {
				resultMts.push(item.mcc)
			}
		}
	}
	if (resultMts.length === 0 && resultMeetings.length === 0 && dcsList.length === 0) {
		Alert(t('请先选择需要删除的终端'))
		return
	}
	let num = resultMts.length + resultMeetings.length + dcsList.length
	Confirm(t('是否删除已选择的 {{num}} 个终端？', { num: num }), () => {
		const meetingMoid = useMccStore.getState().mccInfo.meetingMoid
		if (getCurrentTabType() === 'dcs') {
			fetchDeleteDcsMts(meetingMoid, dcsList)
		} else {
			fetchDeleteMt(meetingMoid, [...resultMts, ...resultMeetings])
		}
	})
}
//startVmp
export const actionButtonsStartMtVmp = () => {
	let resultMts: Mt[] = []
	if (getCurrentTabType() === 'all') {
		const tree = useTableAllStore.getState().tree
		const isSuperMeeting = useMccStore.getState().mccInfo.superMtNum
		getItemInTree(tree, (item) => {
			if (isSuperMeeting) {
				if (item.mcc.isMt && item.status.isChecked && item.mcc.status.isInteractive) {
					resultMts.push(item.mcc)
				}
			} else {
				if (item.mcc.isMt && item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			}
		})
		// return resultMts
	} else if (getCurrentTabType() === 'often') {
		const list = useTableOftenStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'interactive') {
		const list = useTableInteractiveStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'dcs') {
		const list = useTableDcsStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'org') {
		const list = useTableOrgStore.getState().list
		const isSuperMeeting = useMccStore.getState().mccInfo.superMtNum
		for (const item of list) {
			if (isSuperMeeting) {
				if (item.status.isChecked && item.mcc.status.isInteractive) {
					resultMts.push(item.mcc)
				}
			} else {
				if (item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			}
		}
	}
	if (resultMts.length === 0) {
		Alert(t('请先选择录像终端'))
		return
	}
	useMccMtVideoStore.getState().startMtsVideo(
		resultMts.map((x) => ({ mt: revert_mt(x) })),
		'start'
	)
}
//createMtVmp
export const actionButtonsCreateMtVmp = () => {
	let resultMts: Mt[] = []
	if (getCurrentTabType() === 'all') {
		const tree = useTableAllStore.getState().tree
		const isSuperMeeting = useMccStore.getState().mccInfo.superMtNum
		getItemInTree(tree, (item) => {
			if (isSuperMeeting) {
				if (item.mcc.isMt && item.status.isChecked && item.mcc.status.isInteractive) {
					resultMts.push(item.mcc)
				}
			} else {
				if (item.mcc.isMt && item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			}
		})
	} else if (getCurrentTabType() === 'often') {
		const list = useTableOftenStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'interactive') {
		const list = useTableInteractiveStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'dcs') {
		const list = useTableDcsStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'org') {
		const list = useTableOrgStore.getState().list
		const isSuperMeeting = useMccStore.getState().mccInfo.superMtNum
		for (const item of list) {
			if (isSuperMeeting) {
				if (item.status.isChecked && item.mcc.status.isInteractive) {
					resultMts.push(item.mcc)
				}
			} else {
				if (item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			}
		}
	}
	if (resultMts.length === 0) {
		Alert(t('请先选择录像终端'))
		return
	}
	useMccMtVideoStore.getState().createMtsVideo(
		resultMts.map((x) => ({ mt: revert_mt(x) })),
		'create'
	)
}
//添加
export const actionButtonsAdd = () => {
	let resultMts: Mt[] = []
	if (getCurrentTabType() === 'all') {
		const tree = useTableAllStore.getState().tree
		const isSuperMeeting = useMccStore.getState().mccInfo.superMtNum
		getItemInTree(tree, (item) => {
			if (isSuperMeeting) {
				if (item.mcc.isMt && item.status.isChecked && item.mcc.status.isInteractive) {
					resultMts.push(item.mcc)
				}
			} else {
				if (item.mcc.isMt && item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			}
		})
	} else if (getCurrentTabType() === 'often') {
		const list = useTableOftenStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'interactive') {
		const list = useTableInteractiveStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'dcs') {
		const list = useTableDcsStore.getState().list
		resultMts = list.filter((item) => item.status.isChecked).map((x) => x.mcc)
	} else if (getCurrentTabType() === 'org') {
		const list = useTableOrgStore.getState().list
		const isSuperMeeting = useMccStore.getState().mccInfo.superMtNum
		for (const item of list) {
			if (isSuperMeeting) {
				if (item.status.isChecked && item.mcc.status.isInteractive) {
					resultMts.push(item.mcc)
				}
			} else {
				if (item.status.isChecked) {
					resultMts.push(item.mcc)
				}
			}
		}
	}
	if (resultMts.length === 0) {
		Alert(t('请先选择需要添加的终端'))
		return
	}
	if (useMccStore.getState().curPage === 'rollcall') {
		if (useRollCallStore.getState().inputKeyUp.length > 0) {
			useRollCallStore.setState({ inputKeyUp: '' })
		}
		useRollCallStore.getState().getSearchResult('', (data) => {
			useRollCallStore.getState().searchSuccessCall(data)
			addAction(resultMts.map((x) => revert_mt(x)))
		})
	} else {
		addAction(resultMts.map((x) => revert_mt(x)))
	}
	// }
	actionButtonsUnCheckAll()
}
function addAction(mts: MtEntity[]) {
	const curPage = useMccStore.getState().curPage
	switch (curPage) {
		case 'mix':
			useMixStore.getState().batchAddList(mts)
			break
		case 'vip':
			useVipStore.getState().add(mts)
			break
		case 'msg':
			useSmsStore.getState().addList(mts)
			break
		case 'rollcall':
			// 添加到点名轮询时，限制5000方
			if (useRollCallStore.getState().list.length + mts.length > 5000) {
				Alert(t('参与点名与会方最多为5000方'))
				return
			}
			useRollCallStore.getState().addList(mts)
			useRollCallStore.getState().changeCalleeList()
			break
		case 'recall':
			useChasingStore.getState().addList(mts)
			break
		case 'chairman_poll':
		case 'meeting_poll':
		case 'upload':
			if (curPage === 'meeting_poll') {
				//会议轮询刷新左侧图标
				usePollStore.getState().getCurList()
			}
			usePollStore.getState().addList(mts)
			usePollStore.getState().updateList([])
			usePollStore.getState().saveStartList()
			break
	}
}
